package com.gdut.laiagent.rag;

import jakarta.annotation.Resource;
import org.springframework.ai.document.Document;
import org.springframework.ai.embedding.EmbeddingModel;
import org.springframework.ai.vectorstore.SimpleVectorStore;
import org.springframework.ai.vectorstore.VectorStore;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.List;

/**
 * 向量数据库配置，初始化基于内存的向量数据库Bean
 */
@Configuration
public class LoveAppVectorStoreConfig {

    @Resource
    LoveAppDocumentLoader loveAppDocumentLoader;

    // 引入切词器
    @Resource
    MyTokenTextSplitter myTokenTextSplitter;

    // 引入元信息增强器
    @Resource
    MyKeywordEnricher myKeywordEnricher;

    // 加载文档，并将文档转换为向量，并存储到内存中的 SimpleVectorStore
    @Bean
    VectorStore loveAppVectorStore(EmbeddingModel dashscopeEmbeddingModel){
        // 使用dashscopeEmbeddingModel模型创建的内存向量数据库simpleVectorStore
        SimpleVectorStore simpleVectorStore = SimpleVectorStore.builder(dashscopeEmbeddingModel).build();
        List<Document> documentList = loveAppDocumentLoader.loadMarkdowns();
        // 用自定义的文档切分方法
//        List<Document> splitDocument = myTokenTextSplitter.splitCustomized(documentList);

        // 自动补充关键词元信息
        List<Document> enrichDocuments = myKeywordEnricher.enrichDocuments(documentList);

        // 将一组文档（Document 对象）转换为向量（Embedding）并存储到向量数据库中
        simpleVectorStore.add(enrichDocuments);
        return simpleVectorStore;
    }
}
